package com.xiaoyun.aspect;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.hibernate.validator.constraints.CodePointLength;
import org.springframework.stereotype.Component;

@Aspect
@Component
@Slf4j
public class ServiceAspect {

    @Around("execution(* com.xiaoyun.service.impl..*.*(..))")
    public Object recordTimeLog(ProceedingJoinPoint joinPoint) throws Throwable {
        log.info("========开始执行:{},{}=======",
                joinPoint.getTarget().getClass(),
                joinPoint.getSignature().getName());

        long begin = System.currentTimeMillis();

        Object result = joinPoint.proceed();
        long end = System.currentTimeMillis();
        long takeTime = end - begin;
        if (takeTime > 3000L) {
            log.error("===执行结束,耗时:{} 毫秒=====", takeTime);
        } else if (takeTime > 2000L) {
            log.warn("===执行结束,耗时:{} 毫秒=====", takeTime);
        } else {
            log.info("===执行结束,耗时:{} 毫秒=====", takeTime);
        }
        return result;
    }
}
